$(function() {
	var levelTip1 = $("#category1").attr("data-tip"); //一级分类提示
	var levelTip2 = $("#category2").attr("data-tip"); //二级分类提示
	var levelTip3 = $("#category3").attr("data-tip"); //三级分类提示
	var idNotSelect = "0"; //不选分类时的ID
	var addressDataUrl = $("#addressDataUrl").val();
	var $body = $('body');
	var sAlert = new shortAlert($body)

	//-----------------保存地址-----------------
	$("#save").on("click", function() {
		try {
			var realName = $("#realName").val();
			var phone = $("#phone").val();
			var order_sn = $("#order_sn").val();
			var address = $("#address").val();
			var c1 = $("#category1 option:selected");
			var c2 = $("#category2 option:selected");
			var c3 = $("#category3 option:selected");
			var pid = c1.attr("data-id");
			var cid = c2.attr("data-id");
			var aid = c3.attr("data-id");

			var reg = /^1[3|4|5|7|8]\d{9}$/g;
	
			if(!realName){
				// General.alert("请输入收货人姓名");
				sAlert.show("请输入收货人姓名");
				return;
			}
			if(!phone){
				// General.alert("请输入手机号码");
				sAlert.show("请输入手机号码");
				return;
			} else if (!reg.test(phone)) {
				// General.alert("您填写的手机号码有误！！");
				sAlert.show("您填写的手机号码有误");
				return;
			}
			if(pid=="0"){
				// General.alert("请选择省份");
				sAlert.show("请选择省份");
				return;
			}
			if(cid=="0"){
				// General.alert("请选择城市");
				sAlert.show("请选择城市");
				return;
			}
			if(aid=="0"){
				sAlert.show("请选择区域");
				// General.alert("请选择区域");
				return;
			}
			if(!address){
				sAlert.show("请输入详细地址");
				// General.alert("请输入详细地址");
				return;
			}
			var province = c1.attr("value");
			var city = c2.attr("value");
			var area = c3.attr("value");
			var type = $("#addressType").val();
			var defaultId=0;
			if($('#setDefault1').prop("checked")){
				defaultId=1;
			}
			var data = {
				type: type,
				consignee: realName,
				phone: phone,
                order_sn: order_sn,
				province: province,
				city: city,
				address: address,
				street: area,
				defaultId:defaultId
			};
			if (type == "edit") {
				data["id"] = $("#addressId").val();
			}
			var url = $(this).attr("data-href"); //保存的地址
			var tip = "正在保存地址"
			var isHideLoading = false;
			var successFun = null;
			//			var params=province+city+area+address;
			var params = data;
			var isHideResponse = true; //是否不弹出结果
			// Common.ajaxPostWantResult(url, data, tip, null, null, isHideLoading, successFun, params, isHideResponse);
			Common.ajaxPostWantResult(url, data, tip, null, null, isHideLoading, function(data){
				sAlert.show(data.msg);
			}, params, isHideResponse);
		} catch (e) {}
	});
	//保存成功后执行的操作
	function saveResult(data) {
//		console.log(data);
		try {
			var id = data["id"]; //服务器返回的id
			$("#defaultAdressContainer").removeClass("hide");
			$("#adressContainer").addClass("hide");
			$("#addressId").val(id);
			var params = data["successParam"];
			$("#defaultName").html(params["name"]);
			$("#defaultPhone").html(params["phone"]);
			var address = params["province"] + params["city"] + params["street"] + params["address"];
			$("#defaultAddress").html(address);
		} catch (e) {
		}
	};
	//-----------------省市区关联操作-----------------
	
	//初始化一级分类
	function initCategory() {
		//根据一级分类切换二级分类
		$("#category1").on("change", function(e) {
			changeCategory();
		});
		//根据二级分类切换三级分类
		$("#category2").on("change", function(e) {
			changeCategory2();
		});
	};
	//根据一级分类切换二级分类
	function changeCategory() {
		var id = $("#category1 option:selected").attr("data-id");
		var str = '';
		str += '<option value="' + levelTip2 + '" data-id="' + idNotSelect + '" selected>' + levelTip2 + '</option>';
		$("#category2").html(str);
		var str3 = '<option value="' + levelTip3 + '" data-id="' + idNotSelect + '" selected>' + levelTip3 + '</option>';
		$("#category3").html(str3);
		if (id !== "0") {
			$("#category2,#category3").prop("disabled", "disabled");
			var url = addressDataUrl + "?type=city&id=" + id;
			//加载市
			$.ajax({
				type: "GET",
				url: url,
				cache: false,
				dataType: 'jsonp',
				success: function(data) {
					try {
						$.each(data.city, function(key, value) {
							str += '<option value="' + value + '" data-id="' + key + '">' + value + '</option>';
							$("#category2").html(str).prop("disabled", false);
						});
					} catch (e) {}
				},
				error: function() {
					try {
						// General.alert("获取市失败,请检查网络连接");
						sAlert.show("获取市失败,请检查网络连接");
					} catch (e) {}
				}
			});
		} else {

		}
	};
	//根据二级分类切换三级分类
	function changeCategory2() {
		var id = $("#category2 option:selected").attr("data-id");
		var str = '';
		str += '<option value="' + levelTip3 + '" data-id="' + idNotSelect + '" selected>' + levelTip3 + '</option>';
		$("#category3").html(str);
		if (id !== "0") {
			$("#category3").prop("disabled", "disabled");
			var url = addressDataUrl + "?type=area&id=" + id;
			//加载市
			$.ajax({
				type: "GET",
				url: url,
				cache: false,
				dataType: 'jsonp',
				success: function(data) {
					try {
						$.each(data.area, function(key, value) {
							str += '<option value="' + value + '" data-id="' + key + '">' + value + '</option>';
							$("#category3").html(str).prop("disabled", false);
						});
					} catch (e) {}
				},
				error: function() {
					try {
						// General.alert("获取区域失败,请检查网络连接");
						sAlert.show("获取区域失败,请检查网络连接");
						
					} catch (e) {}
				}
			});
		} else {

		}
	}
	initCategory();
});